package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.*;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.BedOrderService;
import com.bestescortbedsystem.util.FindDate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.*;

/**
 * ClassName: BedOrderServiceImpl
 * Package: com.bestescortbedsystem.service.impl
 *
 * @author: guanzepeng
 * @Date: 2021/3/2 15:59 星期二
 * @Description: 床位订单业务层
 */
@Service
public class BedOrderServiceImpl implements BedOrderService {

	@Resource
	private SesBedMessageDao sesBedMessageDao;

	@Resource
	private SesBedTypeDao sesBedTypeDao;

	@Resource
	private SesUserOrdinaryDao sesUserOrdinaryDao;

	@Resource
	private SesUserOrderDao sesUserOrderDao;

	@Resource
	private SesHospitalinfoDao sesHospitalinfoDao;



	/**
	 * 根据医院ID和科室ID查询所对应的床位信息
	 * @param hospitalinfoid 传入医院ID
	 * @param departmentid 传入科室ID
	 * @return 返回Map
	 */
	@Override
	public Map<String, Object> findBed(Long hospitalinfoid, Long departmentid) {
		Map<String, Object> map = new HashMap<>();
		Map<Long, Object> map1 = new HashMap<>();
		if(hospitalinfoid != null && departmentid != null) {
			SesBedMessageExample sesBedMessageExample = new SesBedMessageExample();
			SesBedMessageExample.Criteria criteria = sesBedMessageExample.createCriteria();
			criteria.andHospitalinfoidEqualTo(hospitalinfoid);
			criteria.andHospitalinfoDepartmentidEqualTo(departmentid);
			List<SesBedMessage> list = sesBedMessageDao.selectByExample(sesBedMessageExample);
			for (SesBedMessage s : list) {
				SesBedType bedType = sesBedTypeDao.selectByPrimaryKey(s.getBedBedTypeid());
				map1.put(s.getBedBedTypeid(),bedType);
			}
			map.put("bed",list);
			map.put("bedtype",map1);
		}else {
			List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(null);
			map.put("yiyuan", sesHospitalinfos);
		}
		return map;
	}

	/**
	 * 生成订单
	 * @param bedtypePrice 传入床位单价
	 * @param bedmessageId 传入床位租借信息表ID
	 * @param bedtypeInventory 传入现有库存
	 * @param money 传入总价
	 * @param tian 下单天数
	 * @param userId 传入用户ID
	 * @return 返回Map
	 */
	@Override
	public Map<String, String> byOrder(Long bedtypePrice, Long bedmessageId, Long bedtypeInventory, Float money, String tian, Long userId) {
		Map<String, String> map = new HashMap<>();
		if(bedtypePrice == null || bedmessageId == null || bedtypeInventory == null || userId == null) {
			map.put("s", "失败!");
			return map;
		}
		SesUserOrdinaryExample sesUserOrdinaryExample = new SesUserOrdinaryExample();
		sesUserOrdinaryExample.createCriteria().andUseridEqualTo(userId);
		List<SesUserOrdinary> list = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
		if(list.get(0).getOrdinarymoney() < bedtypePrice) {
			map.put("s", "账户余额不足,请充值!");
			return map;
		}
		if(list.get(0).getOrdinarydeposit() < 300) {
			map.put("s", "您的押金不足300元,请先补足押金!");
			return map;
		}
		if(bedtypeInventory - 1 < 0) {
			map.put("s", "库存不足!");
			return map;
		}
		SesBedMessage message = sesBedMessageDao.selectByPrimaryKey(bedmessageId);
		Long bedBedTypeid = message.getBedBedTypeid();
		SesBedType sesBedType = new SesBedType();
		sesBedType.setBedtypeinventory(bedtypeInventory - 1);
		SesBedTypeExample sesBedTypeExample = new SesBedTypeExample();
		sesBedTypeExample.createCriteria().andBedtypeidEqualTo(bedBedTypeid);

		sesBedTypeDao.updateByExampleSelective(sesBedType, sesBedTypeExample);
		SesUserOrder sesUserOrder = new SesUserOrder();
		sesUserOrder.setBedMessageid(bedmessageId);
		sesUserOrder.setUserUserid(userId);
		UUID uuid = UUID.randomUUID();
		sesUserOrder.setUserordernum(uuid.toString());
		sesUserOrder.setUserorderstatus(1);
		sesUserOrder.setUserorderstarttime(FindDate.getDate());
		sesUserOrder.setUserordersettlementstatus("1");
		sesUserOrder.setUserorderrent(Float.valueOf(bedtypePrice));
		sesUserOrder.setUserordernumber(1L);
		sesUserOrder.setUserorderrent(money);
		sesUserOrder.setUserorderleasetime(tian);
		boolean b = sesUserOrderDao.insert(sesUserOrder) == 1;
		if(b) {
			SesBedMessageExample sesBedMessageExample = new SesBedMessageExample();
			sesBedMessageExample.createCriteria().andBedmessageidEqualTo(bedmessageId);
			SesBedMessage sesBedMessage = new SesBedMessage();
			sesBedMessage.setUserid(userId);
			b = sesBedMessageDao.updateByExampleSelective(sesBedMessage, sesBedMessageExample) == 1;
			if(b) {
				SesUserOrderExample sesUserOrderExample = new SesUserOrderExample();
				sesUserOrderExample.createCriteria().andBedMessageidEqualTo(bedmessageId);
				List<SesUserOrder> orders = sesUserOrderDao.selectByExample(sesUserOrderExample);
				String userorderid = String.valueOf(orders.get(0).getUserorderid());
				map.put("s","下单成功!");
				map.put("userorderid",userorderid);
				return map;
			}
			map.put("s","下单失败!");
			return map;
		}
		map.put("s","失败!");
		return map;
	}

	/**
	 * 修改订单付款状态
	 * @param userorderId 传入订单ID
	 * @param userorderStatus 传入修改状态
	 * @return 返回Boolean
	 */
	@Override
	public Boolean updateStatus(Long userorderId, Integer userorderStatus) {
		if(userorderId == null || userorderStatus == null) {
			return false;
		}
		SesUserOrder sesUserOrder = new SesUserOrder();
		sesUserOrder.setUserorderstatus(userorderStatus);
		SesUserOrderExample sesUserOrderExample = new SesUserOrderExample();
		sesUserOrderExample.createCriteria().andUserorderidEqualTo(userorderId);
		boolean b = sesUserOrderDao.updateByExampleSelective(sesUserOrder, sesUserOrderExample) == 1;
		return b;
	}

	/**
	 * 根据下单天数扣除账户金额
	 * @param money 传入总价
	 * @param tian 下单天数
	 * @param userId 传入用户ID
	 * @param userorderId 传入订单表ID
	 * @return 返回Boolean
	 */
	@Override
	public Boolean updateYuE(Float money, String tian, Long userId, Long userorderId) {
		if(money == null || tian == null || userorderId == null) {
			return false;
		}
		SesUserOrdinaryExample sesUserOrdinaryExample = new SesUserOrdinaryExample();
		sesUserOrdinaryExample.createCriteria().andUseridEqualTo(userId);
		List<SesUserOrdinary> sesUserOrdinaries = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
		SesUserOrdinary sesUserOrdinary = sesUserOrdinaries.get(0);
		if(sesUserOrdinary.getOrdinarymoney() < money) {
			return false;
		}
		sesUserOrdinary.setOrdinarymoney(sesUserOrdinary.getOrdinarymoney() - money);
		SesUserOrderExample sesUserOrderExample = new SesUserOrderExample();
		sesUserOrderExample.createCriteria().andUserorderidEqualTo(userorderId);
		SesUserOrder sesUserOrder = new SesUserOrder();
		sesUserOrder.setUserorderleasetime(tian);
		sesUserOrder.setUserorderstatus(2);
		sesUserOrder.setUserorderrent(money);
		boolean b = sesUserOrderDao.updateByExampleSelective(sesUserOrder, sesUserOrderExample) == 1;
		return b;
	}


}
